package com.tarim.wheat.util;

import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.*;


/**
 * 上传文件工具类
 */
public class UploadFileUtil {

    public static String filePath;

    public static String fileUrl;

    static{
        filePath = "/data/xwj";
//        filePath = "F:\\许文杰\\file";
        fileUrl = "https://www.layoom.top/wheat/image/";
//        fileUrl = "http://127.0.0.1:9094/wheat/image/";
    }
    static List<String> IMG_TYPE;
    static {
        IMG_TYPE = new ArrayList<>();
        //文件类型
        IMG_TYPE.add("PNG");
        IMG_TYPE.add("JPG");
        IMG_TYPE.add("JPEG");
        IMG_TYPE.add("BMP");
        IMG_TYPE.add("GIF");
        IMG_TYPE.add("SVG");
    }

    public static List<Map> upload(MultipartFile[] files) throws IOException {
        List<Map> fileNew = new ArrayList<>();
        for (MultipartFile file : files){
            Map map = new HashMap();
            // 重构文件名称
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            if(!IMG_TYPE.contains(suffix.toUpperCase())){
                map.put("code", 500);
                map.put("fileName", file.getOriginalFilename());
            }
            String pikId = UUID.randomUUID().toString().replaceAll("-", "");
            String newFileName = pikId + "." + suffix;
            File dest = new File(filePath);
            //判断文件父目录是否存在
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            //保存文件
            File fileSave = new File(filePath, newFileName);
            file.transferTo(fileSave);
            map.put("code", 200);
            map.put("fileName", file.getOriginalFilename());
            map.put("fileUrl", fileUrl + newFileName);
            map.put("fileSize", file.getSize());
            fileNew.add(map);
        }
        return fileNew;
    }

    public static synchronized Map upload(MultipartFile file) throws IOException {
        // 重构文件名称
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        if(!IMG_TYPE.contains(suffix.toUpperCase())){
            return null;
        }
        String pikId = UUID.randomUUID().toString().replaceAll("-", "");
        String newFileName = pikId + "." + suffix;
        File dest = new File(filePath);
        //判断文件父目录是否存在
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        //保存文件
        File fileSave = new File(filePath, newFileName);
        file.transferTo(fileSave);
        Map map = new HashMap();
        map.put("fileName", file.getOriginalFilename());
        map.put("fileUrl", fileUrl + newFileName);
        map.put("fileSize", file.getSize());
        return map;
    }


    public static String getImageBase64code(String iamgePath) {
        try {
            File file = new File(iamgePath);
            FileInputStream fs;

            fs = new FileInputStream(file);

            byte[] data = new byte[fs.available()];
            fs.read(data);
            fs.close();
            BASE64Encoder be = new BASE64Encoder();

            return be.encode(data);
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    public static String base64CodeToimage(String basee64code) throws IOException {
        if (basee64code == null) {
            return null;// 图像数据为空
        }
        BASE64Decoder decoder = new BASE64Decoder();
        //byte[] b = decoder.decodeBuffer(basee64code.replace("data:image/jpeg;base64,", ""));//去除base64中无用的部分
        OutputStream out = null;
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        String newFileName = "";
        if (basee64code.indexOf("data:image/png;") != -1) {
            newFileName = id + ".png";
        } else if (basee64code.indexOf("data:image/jpeg;") != -1) {
            newFileName = id + ".jpeg";
        } else if (basee64code.indexOf("data:image/gif;") != -1) {
            newFileName = id + ".gif";
        } else {
            return null;
        }
        try {
            basee64code.replaceAll(" ", "+");
            basee64code = basee64code.substring(basee64code.indexOf(",") + 1);
            out = new FileOutputStream(filePath + newFileName);
            // Base64解码
            byte[] b = decoder.decodeBuffer(basee64code);

            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            out.write(b);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
            return newFileName;
        }
    }

}
